﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.ObjectMapping;
using Abp.Runtime.Caching;
using AVIS.OutBound.RentalPolicys.Cache;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.OutBound.RentalPolicys
{
    public class RentalPolicyManager : DomainService, IRentalPolicyManager
    {

        private readonly IRepository<RentalPolicy> _rentalPolicyRepository;
        private readonly IObjectMapper _objectMapper;
        private readonly ICacheManager _cacheManager;

        public RentalPolicyManager(IRepository<RentalPolicy> rentalPolicyRepository,
            IObjectMapper objectMapper,
            ICacheManager cacheManager)
        {
            _rentalPolicyRepository = rentalPolicyRepository;
            _objectMapper = objectMapper;
            _cacheManager = cacheManager;
        }

        public RentalPolicyCacheItem GetRentalPolicyByRegionCode(string regionCode)
        {
            var cacheItem = _cacheManager.GetRentalPolicyCache().Get(regionCode, f => RentalPolicy(regionCode));
            return cacheItem;
        }

        /// <summary>
        /// 获取指定区域的租车信息
        /// </summary>
        /// <param name="regionCode"></param>
        /// <returns></returns>
        protected RentalPolicyCacheItem RentalPolicy(string regionCode)
        {
            var RentalPolicyData = _rentalPolicyRepository.FirstOrDefault(c => c.RegionCode == regionCode);
            var Rental = _objectMapper.Map<RentalPolicyCacheItem>(RentalPolicyData);
            return Rental;
        }
    }
}
